﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace LGPSDataGen.Generators
{
    class EaglesGenerator : Generator, IGenerator
    {
        private string pathToFile;
        //Have to use Wikipedia as open data source because traditional ESPN source is too complex
        private string content = "http://en.wikipedia.org/wiki/2010_Philadelphia_Eagles_season";
        private DateTime mDate;

        public EaglesGenerator(DateTime theDate)
        {
            mDate = theDate;
            string timeStamp = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
            pathToFile = @"Eagles" + timeStamp + ".html";
        }

        public override void DownloadData()
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(content, pathToFile);
        }

        public override void ProcessData()
        {
            StreamReader stream = new StreamReader(pathToFile + ".txt");

            bool found = false;
            string fullDate = "";
            List<string> dataArray = new List<string>();

            while (!stream.EndOfStream)
            {
                string s = stream.ReadLine();
                int month = 0;
                int day = 0;
                int hour = 0;

                //If we find a month, continue to process (there is a addtional data contained within the wikipedia page
                if (DateUtility.IsMonthString(s))
                {
                    //Build the correct date from the string provided
                    month = DateUtility.ConvertMonthToInt(s);
                    day = Int32.Parse(s.Split(' ')[1]);
                    s = stream.ReadLine();
                    hour = Int32.Parse(s.Split(':')[0]);
                    hour = hour + DateUtility.AMorPM(s); //add 12 if nesecary
                    DateTime d = new DateTime(2011, month, day, hour, 0, 0);

                    fullDate = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;

                    //Determine if this is correct for the day we are looking for
                    //NFL games typically last for 3 hours
                    if (d.DayOfYear == mDate.DayOfYear)
                    {
                        found = true;
                        for (int i = 0; i < 24; i++)
                        {
                            string data;
                            if ((i == hour) || (i > hour && i < hour + 3))
                                data = i + ",1";
                            else
                                data = i + ",0";
                            dataArray.Add(data);
                        }
                    }
                }
            }
            if(!found) //If the date we are looking for is not found, make the date all 0's
            {
                fullDate = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
                for (int i = 0; i < 24; i++)
                {
                    string data = i + ",0";
                    dataArray.Add(data);
                }
            }
            //Output CSV file
            File.WriteAllLines(@"Eagles-" + fullDate + ".csv", dataArray.ToArray());
        }

        public override void ExtractData()
        {
            ExtractSeasonInfo();
        }

        private void ExtractSeasonInfo()
        {
            StreamReader stream = new StreamReader(pathToFile);
            StreamWriter writer = new StreamWriter(pathToFile + ".txt");

            while (!stream.EndOfStream)
            {
                string s = stream.ReadLine();
                if (s.Contains("Edit section: Regular season")) //Hard string from Wikipedia
                {
                    break;
                }
            }

            while (!stream.EndOfStream)
            {
                string s = stream.ReadLine();
                if (s.Contains("<th>")) //Find the table heading tag, this is where the season is
                {
                    writer.WriteLine(Regex.Replace(stream.ReadLine(), "<.*?>", string.Empty));
                    writer.WriteLine(Regex.Replace(stream.ReadLine(), "<.*?>", string.Empty));
                }
                if (s.Contains(@"</table>"))
                    break;
            }

            writer.Close();
            stream.Close();
        }
    }
}
